package com.smxy.mall.controller;

import com.smxy.mall.common.Response;
import com.smxy.mall.entity.Goods;
import com.smxy.mall.model.request.SwiperReq;
import com.smxy.mall.service.GoodsService;
import com.smxy.mall.service.MenuService;
import com.smxy.mall.service.NoticeService;
import com.smxy.mall.service.SwiperService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "基础信息")
@RestController
@RequestMapping("/base")
public class BaseController {
    @Resource
    private NoticeService noticeService;
    @Resource
    private MenuService menuService;
    @Resource
    private SwiperService swiperService;
    @Resource
    private GoodsService goodsService;

    @ApiOperation("平台信息")
    @GetMapping("/info")
    public Object info() {
        Map<String,String> map = new HashMap<>();
        map.put("name","面包小妹");
        map.put("company","胖周颖工作室");
        return Response.success(map);
    }

    @ApiOperation("平台公告")
    @GetMapping("/notice")
    public Object notice() {
        List list = noticeService.list();
        if(list.size()>0){
            return Response.success(list.get(0));
        }else{
            return Response.success("");
        }
    }

    @ApiOperation("平台轮播图")
    @GetMapping("/platformSwipe")
    public Object platformSwipe(@Valid SwiperReq swiperReq) {
        return Response.success(swiperService.list(swiperReq));
    }

    @GetMapping("/menu")
    public Object menu() {
        return Response.success(menuService.list());
    }

    @GetMapping("/home")
    public Object pageData() {
        Sort sort = Sort.by(Sort.Direction.DESC,"id");
        Pageable pageable = PageRequest.of(0,5,sort);
        Page<Goods> ysGoods = goodsService.goodsList(34,pageable);
        Page<Goods> zpGoods = goodsService.goodsList(35,pageable);
        List<Object> list = new ArrayList<>();
        Map<String,Object> ysMap = new HashMap<>();
        Map<String,Object> zpMap = new HashMap<>();
        ysMap.put("type","advert");
        ysMap.put("list",ysGoods.getContent());
        zpMap.put("type","top_food");
        zpMap.put("list",zpGoods.getContent());
        list.add(ysMap);
        list.add(zpMap);
        return Response.success(list);
    }
}
